/*
 * @(#)EmailMessageSenderComparator.java
 *
 * Copyright (c) 2004 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */

package com.dcivision.upgrade;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.ResultSet;

/**
 * A utility tools provides some common methods related JDBC operation.
 * 
 * @author      Brook.Huang
 * @company     DCIVision Limited
 * @creation    19/04/2007
 * @version     $Revision: 1.3 $
 */
public class JDBCHelper {

  private JDBCHelper() {
    // Avoid instantiation.
  }

  public static Object getObjectFromResultSet(ResultSet rs, String colName) throws Exception {
    InputStream inputStream = rs.getBinaryStream(colName);
    ObjectInputStream in = new ObjectInputStream(inputStream);
    Object result = in.readObject();
    in.close();
    return (result);
  }

  public static byte[] object2Bytes(Object val) throws Exception {

    if (val == null) {
      return (null);
    } else {
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      baos = new ByteArrayOutputStream();
      ObjectOutputStream out = new ObjectOutputStream(baos);
      out.writeObject(val);
      out.flush();
      return (baos.toByteArray());
    }
  }
}
